
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>IO编程 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="模块.html" />
    
    
    <link rel="prev" href="异常处理.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础.html">
            
                <a href="基础.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="字符串处理.html">
            
                <a href="字符串处理.html">
            
                    
                    字符串处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="数据结构.html">
            
                <a href="数据结构.html">
            
                    
                    数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="数据结构高级内容.html">
            
                <a href="数据结构高级内容.html">
            
                    
                    数据结构高级内容
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="函数式编程.html">
            
                <a href="函数式编程.html">
            
                    
                    函数式编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="面向对象编程.html">
            
                <a href="面向对象编程.html">
            
                    
                    面向对象编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="类的特殊方法.html">
            
                <a href="类的特殊方法.html">
            
                    
                    类的特殊方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.11" data-path="IO编程.html">
            
                <a href="IO编程.html">
            
                    
                    IO编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="模块.html">
            
                <a href="模块.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="标准库.html">
            
                <a href="标准库.html">
            
                    
                    标准库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="测试.html">
            
                <a href="测试.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="调试.html">
            
                <a href="调试.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >IO编程</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="io&#x7F16;&#x7A0B;">IO&#x7F16;&#x7A0B;</h1>
<hr>
<h2 id="&#x6587;&#x4EF6;&#x8BFB;&#x5199;">&#x6587;&#x4EF6;&#x8BFB;&#x5199;</h2>
<p>Python &#x5185;&#x7F6E;&#x4E86;&#x8BFB;&#x5199;&#x6587;&#x4EF6;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x7528;&#x6CD5;&#x548C;C &#x662F;&#x517C;&#x5BB9;&#x7684;&#x3002;&#x3000;&#x3000;
&#x4EE5;&#x4E0B;&#x5185;&#x5BB9;&#x6CA1;&#x6709;&#x7279;&#x522B;&#x8BF4;&#x660E;&#x5219;&#x9ED8;&#x8BA4;&#x662F;&#x8BFB;&#x53D6;&#x6587;&#x672C;&#x6587;&#x4EF6;&#xFF0C;&#x5E76;&#x4E14;&#x662F; UTF-8 &#x7F16;&#x7801;&#x7684;&#x6587;&#x672C;&#x6587;&#x4EF6;&#x3002;&#x3000;&#x3000;</p>
<h3 id="file-like-object">file-like Object</h3>
<p>&#x50CF; open()&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x7684;&#x8FD9;&#x79CD;&#x6709;&#x4E2A; read()&#x65B9;&#x6CD5;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x5728; Python &#x4E2D;&#x7EDF;&#x79F0;&#x4E3A;<code>file-like Object</code>&#x3002;<br>&#x9664;&#x4E86; file &#x5916;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x662F;&#x5185;&#x5B58;&#x7684;&#x5B57;&#x8282;&#x6D41;&#xFF0C;&#x7F51;&#x7EDC;&#x6D41;&#xFF0C;&#x81EA;&#x5B9A;&#x4E49;&#x6D41;&#x7B49;&#x7B49;&#x3002;<code>file-like Object</code> &#x4E0D;&#x8981;&#x6C42;&#x4ECE;&#x7279;&#x5B9A;&#x7C7B;&#x7EE7;&#x627F;&#xFF0C;&#x53EA;&#x8981;&#x5199;&#x4E2A; <code>read()</code> &#x65B9;&#x6CD5;&#x5C31;&#x884C;&#x3002;  </p>
<h3 id="&#x3000;&#x8BFB;&#x6587;&#x672C;&#x6587;&#x4EF6;">&#x3000;&#x8BFB;&#x6587;&#x672C;&#x6587;&#x4EF6;</h3>
<p>&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-python">f = open(<span class="hljs-string">&apos;/Users/test.txt&apos;</span>, <span class="hljs-string">&apos;r&apos;</span>)   <span class="hljs-comment">#&#x5982;&#x679C;&#x6587;&#x4EF6;&#x4E0D;&#x5B58;&#x5728;&#xFF0C; open()&#x51FD;&#x6570;&#x5C31;&#x4F1A;&#x629B;&#x51FA;&#x4E00;&#x4E2A; IOError &#x7684;&#x9519;&#x8BEF;</span>
s = f.read()            <span class="hljs-comment">#&#x8C03;&#x7528; read()&#x65B9;&#x6CD5;&#x4E00;&#x6B21;&#x6027;&#x8BFB;&#x53D6;&#x6240;&#x6709;&#x7684;&#x5185;&#x5BB9;&#x5230;&#x5185;&#x5B58;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;str&#x5BF9;&#x8C61;</span>
print(s)
f.close()               <span class="hljs-comment">#&#x5173;&#x95ED;&#x6587;&#x4EF6;</span>
</code></pre>
<blockquote>
<p>&#x6CE8;&#xFF1A;
&#x8C03;&#x7528; read()&#x4F1A;&#x4E00;&#x6B21;&#x6027;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#x7684;&#x5168;&#x90E8;&#x5185;&#x5BB9;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;str&#xFF1B;
&#x8C03;&#x7528; read(size)&#x6BCF;&#x6B21;&#x6700;&#x591A;&#x8BFB;&#x53D6;size&#x4E2A;&#x5B57;&#x8282;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;str&#xFF1B;
&#x8C03;&#x7528; readline()&#x6BCF;&#x6B21;&#x8BFB;&#x53D6;&#x4E00;&#x884C;&#x5185;&#x5BB9;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;str&#xFF1B;
&#x8C03;&#x7528; readlines()&#x4F1A;&#x4E00;&#x6B21;&#x6027;&#x8BFB;&#x53D6;&#x6240;&#x6709;&#x5185;&#x5BB9;&#xFF0C;&#x5E76;&#x6309;&#x884C;&#x8FD4;&#x56DE; list&#xFF1B;
&#x5982;&#x679C;&#x6587;&#x4EF6;&#x5F88;&#x5C0F;&#xFF0C; read()&#x4E00;&#x6B21;&#x6027;&#x8BFB;&#x53D6;&#x6700;&#x65B9;&#x4FBF;&#xFF1B;&#x5982;&#x679C;&#x4E0D;&#x80FD;&#x786E;&#x5B9A;&#x6587;&#x4EF6;&#x5927;&#x5C0F;&#xFF0C;&#x53CD;&#x590D;&#x8C03;&#x7528; read(size)&#x6BD4;&#x8F83;&#x4FDD;&#x9669;&#xFF1B;&#x5982;&#x679C;&#x662F;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF0C;&#x8C03;&#x7528; readlines()&#x6700;&#x65B9;&#x4FBF;&#x3002;</p>
</blockquote>
<h3 id="&#x5B57;&#x7B26;&#x7F16;&#x7801;">&#x5B57;&#x7B26;&#x7F16;&#x7801;</h3>
<p>&#x8981;&#x8BFB;&#x53D6;&#x975E; UTF-8 &#x7F16;&#x7801;&#x7684;&#x6587;&#x672C;&#x6587;&#x4EF6;&#xFF0C;&#x9700;&#x8981;&#x7ED9; <code>open()</code>&#x51FD;&#x6570;&#x4F20;&#x5165; encoding &#x53C2;&#x6570;&#xFF0C;&#x4F8B;&#x5982;&#xFF0C;&#x8BFB;&#x53D6; GBK &#x7F16;&#x7801;&#x7684;&#x6587;&#x4EF6;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>f = open(<span class="hljs-string">&apos;/Users/michael/gbk.txt&apos;</span>, <span class="hljs-string">&apos;r&apos;</span>, encoding=<span class="hljs-string">&apos;gbk&apos;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>f.read()
<span class="hljs-string">&apos;&#x6D4B;&#x8BD5;&apos;</span>
</code></pre>
<p>&#x9047;&#x5230;&#x6709;&#x4E9B;&#x7F16;&#x7801;&#x4E0D;&#x89C4;&#x8303;&#x7684;&#x6587;&#x4EF6;&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x4F1A;&#x9047;&#x5230; UnicodeDecodeError&#xFF0C;&#x56E0;&#x4E3A;&#x5728;&#x6587;&#x672C;&#x6587;&#x4EF6;&#x4E2D;&#x53EF;&#x80FD;&#x5939;&#x6742;&#x4E86;&#x4E00;&#x4E9B;&#x975E;&#x6CD5;&#x7F16;&#x7801;&#x7684;&#x5B57;&#x7B26;&#x3002;&#x9047;&#x5230;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#xFF0C; <code>open()</code>&#x51FD;&#x6570;&#x8FD8;&#x63A5;&#x6536;&#x4E00;&#x4E2A; errors &#x53C2;&#x6570;&#xFF0C;&#x8868;&#x793A;&#x5982;&#x679C;&#x9047;&#x5230;&#x7F16;&#x7801;&#x9519;&#x8BEF;&#x540E;&#x5982;&#x4F55;&#x5904;&#x7406;&#x3002;&#x6700;&#x7B80;&#x5355;&#x7684;&#x65B9;&#x5F0F;&#x662F;&#x76F4;&#x63A5;&#x5FFD;&#x7565;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>f = open(<span class="hljs-string">&apos;/Users/michael/gbk.txt&apos;</span>, <span class="hljs-string">&apos;r&apos;</span>, encoding=<span class="hljs-string">&apos;gbk&apos;</span>,errors=<span class="hljs-string">&apos;ignore&apos;</span>)
</code></pre>
<h3 id="&#x8BFB;&#x4E8C;&#x8FDB;&#x5236;&#x6587;&#x4EF6;">&#x8BFB;&#x4E8C;&#x8FDB;&#x5236;&#x6587;&#x4EF6;</h3>
<p>&#x8981;&#x8BFB;&#x53D6;&#x4E8C;&#x8FDB;&#x5236;&#x6587;&#x4EF6;&#xFF0C;&#x6BD4;&#x5982;&#x56FE;&#x7247;&#x3001;&#x89C6;&#x9891;&#x7B49;&#x7B49;&#xFF0C;&#x7528;<code>&apos;rb&apos;</code>&#x6A21;&#x5F0F;&#x6253;&#x5F00;&#x6587;&#x4EF6;&#x5373;&#x53EF;&#x3002;<br>&#x4F8B;&#x5982;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>f = open(<span class="hljs-string">&apos;/Users/michael/test.jpg&apos;</span>, <span class="hljs-string">&apos;rb&apos;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>f.read()
<span class="hljs-string">b&apos;\xff\xd8\xff\xe1\x00\x18Exif\x00\x00...&apos;</span> <span class="hljs-comment"># &#x5341;&#x516D;&#x8FDB;&#x5236;&#x8868;&#x793A;&#x7684;&#x5B57;&#x8282;</span>
</code></pre>
<h3 id="&#x5199;&#x6587;&#x4EF6;">&#x5199;&#x6587;&#x4EF6;</h3>
<p>&#x4F20;&#x5165;&#x6807;&#x8BC6;&#x7B26;<code>&apos;w&apos;</code>&#x6216;&#x8005;<code>&apos;wb&apos;</code>&#x5206;&#x522B;&#x8868;&#x793A;&#x5199;&#x6587;&#x672C;&#x6587;&#x4EF6;&#x548C;&#x5199;&#x4E8C;&#x8FDB;&#x5236;&#x6587;&#x4EF6;&#x3002;<br>&#x4F8B;&#x5982;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-keyword">with</span> open(<span class="hljs-string">&apos;/Users/michael/test.txt&apos;</span>, <span class="hljs-string">&apos;w&apos;</span>) <span class="hljs-keyword">as</span> f:
    f.write(<span class="hljs-string">&apos;Hello, world!&apos;</span>)
</code></pre>
<h2 id="stringio-&#x548C;-bytesio">StringIO &#x548C; BytesIO</h2>
<p>StringIO &#x548C; BytesIO &#x662F;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x64CD;&#x4F5C; str &#x548C; bytes &#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x5B83;&#x548C;&#x8BFB;&#x5199;&#x6587;&#x4EF6;&#x5177;&#x6709;&#x4E00;&#x81F4;&#x7684;&#x63A5;&#x53E3;&#x3002;  </p>
<h3 id="stringio">StringIO</h3>
<p>StringIO &#x987E;&#x540D;&#x601D;&#x4E49;&#x5C31;&#x662F;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x8BFB;&#x5199; str&#x3002;<br>&#x9996;&#x5148;&#x9700;&#x8981;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; StringIO&#xFF0C;&#x7136;&#x540E;&#xFF0C;&#x50CF;&#x6587;&#x4EF6;&#x4E00;&#x6837;&#x5199;&#x5165;&#x5373;&#x53EF;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> io <span class="hljs-keyword">import</span> StringIO
f1 = StringIO()
f1.write(<span class="hljs-string">&apos;hello&apos;</span>)
print(f1.getvalue())   <span class="hljs-comment">#getvalue()&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x83B7;&#x5F97;&#x5199;&#x5165;&#x540E;&#x7684; str&#x3002;</span>
f1.close()
f2 = StringIO(<span class="hljs-string">&apos;Hello!\nHi!\nGoodbye!&apos;</span>)
s = f2.readline()
f2.close()
</code></pre>
<h3 id="bytesio">BytesIO</h3>
<p>BytesIO&#x662F;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x8BFB;&#x5199;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#x3002;  </p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> io <span class="hljs-keyword">import</span> BytesIO
<span class="hljs-meta">&gt;&gt;&gt; </span>f1 = BytesIO()
<span class="hljs-meta">&gt;&gt;&gt; </span>f1.write(<span class="hljs-string">&apos;&#x4E2D;&#x6587;&apos;</span>.encode(<span class="hljs-string">&apos;utf-8&apos;</span>))
<span class="hljs-number">6</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>print(f.getvalue())   <span class="hljs-comment">#getvalue()&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x83B7;&#x5F97;&#x5199;&#x5165;&#x540E;&#x7684; str&#x3002;</span>
<span class="hljs-string">b&apos;\xe4\xb8\xad\xe6\x96\x87&apos;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>f1.close()
<span class="hljs-meta">&gt;&gt;&gt; </span>f2 = BytesIO(<span class="hljs-string">b&apos;\xe4\xb8\xad\xe6\x96\x87&apos;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>f2.read()
<span class="hljs-string">b&apos;\xe4\xb8\xad\xe6\x96\x87&apos;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>f2.close()
</code></pre>
<h2 id="&#x64CD;&#x4F5C;&#x6587;&#x4EF6;&#x548C;&#x76EE;&#x5F55;">&#x64CD;&#x4F5C;&#x6587;&#x4EF6;&#x548C;&#x76EE;&#x5F55;</h2>
<p>Python &#x5185;&#x7F6E;&#x7684; os &#x6A21;&#x5757;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8C03;&#x7528;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x63D0;&#x4F9B;&#x7684;&#x63A5;&#x53E3;&#x51FD;&#x6570;&#x3002;&#x9700;&#x8981;&#x6CE8;&#x610F;os &#x6A21;&#x5757;&#x7684;&#x67D0;&#x4E9B;&#x51FD;&#x6570;&#x662F;&#x8DDF;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x76F8;&#x5173;&#x7684;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> os
<span class="hljs-meta">&gt;&gt;&gt; </span>os.name 
<span class="hljs-string">&apos;posix&apos;</span>
</code></pre>
<p>&#x67E5;&#x770B;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x7C7B;&#x578B;&#x3002;&#x5982;&#x679C;&#x8F93;&#x51FA;&#x662F; posix&#xFF0C;&#x5219;&#x662F; Linux&#x3001;Unix &#x6216; Mac OS X&#xFF1B;&#x5982;&#x679C;&#x662F; nt&#xFF0C;&#x5219;&#x662F; Windows&#x3002;</p>
<p>&#x8981;&#x83B7;&#x53D6;&#x8BE6;&#x7EC6;&#x7684;&#x7CFB;&#x7EDF;&#x4FE1;&#x606F;&#xFF0C;&#x53EF;&#x4EE5;&#x8C03;&#x7528; uname()&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-python">&gt;&gt;&gt; os.uname()     #&#x6CE8;&#x610F; uname()&#x51FD;&#x6570;&#x5728; Windows &#x4E0A;&#x4E0D;&#x63D0;&#x4F9B;
posix.uname_result(sysname=&apos;Darwin&apos;, nodename=&apos;MichaelMacPro.local&apos;,release=&apos;14.3.0&apos;, 
version=&apos;Darwin Kernel Version 14.3.0: Mon Mar 23 11:59:05 PDT 2015; 
root:xnu-2782.20.48~5/RELEASE_X86_64&apos;,machine=&apos;x86_64&apos;)
</code></pre>
<h3 id="&#x73AF;&#x5883;&#x53D8;&#x91CF;">&#x73AF;&#x5883;&#x53D8;&#x91CF;</h3>
<p>&#x5728;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#xFF0C;&#x5168;&#x90E8;&#x4FDD;&#x5B58;&#x5728; os.environ &#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x67E5;&#x770B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>os.environ
</code></pre>
<p>&#x8981;&#x83B7;&#x53D6;&#x67D0;&#x4E2A;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x7684;&#x503C;&#xFF0C;&#x53EF;&#x4EE5;&#x8C03;&#x7528; os.environ.get(&apos;&#x53D8;&#x91CF;&#x540D;&apos;)&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>os.environ.get(<span class="hljs-string">&apos;PATH&apos;</span>)
</code></pre>
<h3 id="&#x64CD;&#x4F5C;&#x6587;&#x4EF6;&#x548C;&#x76EE;&#x5F55;">&#x64CD;&#x4F5C;&#x6587;&#x4EF6;&#x548C;&#x76EE;&#x5F55;</h3>
<p>&#x67E5;&#x770B;&#x5F53;&#x524D;&#x76EE;&#x5F55;&#x7684;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>os.path.abspath(<span class="hljs-string">&apos;.&apos;</span>)
<span class="hljs-string">&apos;/Users/michael&apos;</span>
</code></pre>
<p>&#x628A;&#x4E24;&#x4E2A;&#x8DEF;&#x5F84;&#x5408;&#x6210;&#x4E00;&#x4E2A;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>os.path.join(<span class="hljs-string">&apos;/Users/michael&apos;</span>, <span class="hljs-string">&apos;testdir&apos;</span>)
<span class="hljs-string">&apos;/Users/michael/testdir&apos;</span>
</code></pre>
<p>&#x521B;&#x5EFA;&#x548C;&#x5220;&#x9664;&#x76EE;&#x5F55;:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>os.mkdir(<span class="hljs-string">&apos;/Users/michael/testdir&apos;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>os.rmdir(<span class="hljs-string">&apos;/Users/michael/testdir&apos;</span>)
</code></pre>
<p>&#x628A;&#x4E00;&#x4E2A;&#x8DEF;&#x5F84;&#x62C6;&#x5206;&#x4E3A;&#x4E24;&#x90E8;&#x5206;&#xFF0C;&#x540E;&#x4E00;&#x90E8;&#x5206;&#x662F;&#x6700;&#x540E;&#x7EA7;&#x522B;&#x7684;&#x76EE;&#x5F55;&#x6216;&#x6587;&#x4EF6;&#x540D;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>os.path.split(<span class="hljs-string">&apos;/Users/michael/testdir/file.txt&apos;</span>)
(<span class="hljs-string">&apos;/Users/michael/testdir&apos;</span>, <span class="hljs-string">&apos;file.txt&apos;</span>)
</code></pre>
<p>&#x5F97;&#x5230;&#x6587;&#x4EF6;&#x6269;&#x5C55;&#x540D;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>os.path.splitext(<span class="hljs-string">&apos;/path/to/file.txt&apos;</span>)
(<span class="hljs-string">&apos;/path/to/file&apos;</span>, <span class="hljs-string">&apos;.txt&apos;</span>)
</code></pre>
<blockquote>
<p>[&#x6CE8;]&#x8FD9;&#x4E9B;&#x5408;&#x5E76;&#x3001;&#x62C6;&#x5206;&#x8DEF;&#x5F84;&#x7684;&#x51FD;&#x6570;&#x5E76;&#x4E0D;&#x8981;&#x6C42;&#x76EE;&#x5F55;&#x548C;&#x6587;&#x4EF6;&#x8981;&#x771F;&#x5B9E;&#x5B58;&#x5728;&#xFF0C;&#x5B83;&#x4EEC;&#x53EA;&#x5BF9;&#x5B57;&#x7B26;&#x4E32;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#x3002;</p>
</blockquote>
<p>&#x5BF9;&#x6587;&#x4EF6;&#x91CD;&#x547D;&#x540D;:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>os.rename(<span class="hljs-string">&apos;test.txt&apos;</span>, <span class="hljs-string">&apos;test.py&apos;</span>)
</code></pre>
<p>&#x5220;&#x6389;&#x6587;&#x4EF6;:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>os.remove(<span class="hljs-string">&apos;test.py&apos;</span>)
</code></pre>
<p>&#x5217;&#x51FA;&#x5F53;&#x524D;&#x76EE;&#x5F55;&#x4E0B;&#x7684;&#x6240;&#x6709;&#x6587;&#x4EF6;&#x548C;&#x76EE;&#x5F55;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span> os.listdir(<span class="hljs-string">&apos;.&apos;</span>)
</code></pre>
<p>&#x5224;&#x65AD;&#x662F;&#x5426;&#x662F;&#x4E00;&#x4E2A;&#x76EE;&#x5F55;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>os.path.isdir(<span class="hljs-string">&apos;test.py&apos;</span>)
</code></pre>
<p>&#x5224;&#x65AD;&#x662F;&#x5426;&#x662F;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>os.path.isdir(<span class="hljs-string">&apos;test.py&apos;</span>)
</code></pre>
<p>&#x4E3E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment">#&#x5217;&#x51FA;&#x6240;&#x6709;&#x7684;.py &#x6587;&#x4EF6;</span>
[x <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> os.listdir(<span class="hljs-string">&apos;.&apos;</span>) <span class="hljs-keyword">if</span> os.path.isfile(x) <span class="hljs-keyword">and</span> os.path.splitext(x)[<span class="hljs-number">1</span>]==<span class="hljs-string">&apos;.py&apos;</span>]
</code></pre>
<h2 id="&#x5E8F;&#x5217;&#x5316;">&#x5E8F;&#x5217;&#x5316;</h2>
<p>&#x628A;&#x53D8;&#x91CF;&#x4ECE;&#x5185;&#x5B58;&#x4E2D;&#x53D8;&#x6210;&#x53EF;&#x5B58;&#x50A8;&#x6216;&#x4F20;&#x8F93;&#x7684;&#x8FC7;&#x7A0B;&#x79F0;&#x4E4B;&#x4E3A;&#x5E8F;&#x5217;&#x5316;&#xFF0C;&#x5728; Python&#x4E2D;&#x53EB; pickling&#x3002;&#x5E8F;&#x5217;&#x5316;&#x4E4B;&#x540E;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x628A;&#x5E8F;&#x5217;&#x5316;&#x540E;&#x7684;&#x5185;&#x5BB9;&#x5199;&#x5165;&#x78C1;&#x76D8;&#xFF0C;&#x6216;&#x8005;&#x901A;&#x8FC7;&#x7F51;&#x7EDC;&#x4F20;&#x8F93;&#x5230;&#x522B;&#x7684;&#x673A;&#x5668;&#x4E0A;&#x3002;<br>&#x628A;&#x53D8;&#x91CF;&#x5185;&#x5BB9;&#x4ECE;&#x5E8F;&#x5217;&#x5316;&#x7684;&#x5BF9;&#x8C61;&#x91CD;&#x65B0;&#x8BFB;&#x5230;&#x5185;&#x5B58;&#x91CC;&#x79F0;&#x4E4B;&#x4E3A;&#x53CD;&#x5E8F;&#x5217;&#x5316;&#xFF0C;&#x5373; unpickling&#x3002;  </p>
<h3 id="pickle-&#x6A21;&#x5757;">pickle &#x6A21;&#x5757;</h3>
<p><code>pickle.dumps()</code> &#x65B9;&#x6CD5;&#x628A;&#x4EFB;&#x610F;&#x5BF9;&#x8C61;&#x5E8F;&#x5217;&#x5316;&#x6210;&#x4E00;&#x4E2A; bytes&#xFF0C;&#x7136;&#x540E;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x628A;&#x8FD9;&#x4E2A; bytes &#x5199;&#x5165;&#x6587;&#x4EF6;&#x3002;<br>&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> pickle
<span class="hljs-meta">&gt;&gt;&gt; </span>d = dict(name=<span class="hljs-string">&apos;Bob&apos;</span>, age=<span class="hljs-number">20</span>, score=<span class="hljs-number">88</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>pickle.dumps(d)
<span class="hljs-string">b&apos;\x80\x03}q\x00(X\x03\x00\x00\x00ageq\x01K\x14X\x05\x00\x00\x00score
q\x02KXX\x04\x00\x00\x00nameq\x03X\x03\x00\x00\x00Bobq\x04u.&apos;</span>
</code></pre>
<p>&#x6216;&#x8005;&#x7528;&#x53E6;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5; <code>pickle.dump()</code>&#x76F4;&#x63A5;&#x628A;&#x5BF9;&#x8C61;&#x5E8F;&#x5217;&#x5316;&#x540E;&#x5199;&#x5165;&#x4E00;&#x4E2A; <code>file-like Object</code>&#xFF1A;</p>
<pre><code class="lang-python">f = open(<span class="hljs-string">&apos;dump.txt&apos;</span>, <span class="hljs-string">&apos;wb&apos;</span>)
pickle.dump(d, f)
f.close()
</code></pre>
<p>&#x5F53;&#x6211;&#x4EEC;&#x8981;&#x628A;&#x5BF9;&#x8C61;&#x4ECE;&#x78C1;&#x76D8;&#x8BFB;&#x5230;&#x5185;&#x5B58;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x5148;&#x628A;&#x5185;&#x5BB9;&#x8BFB;&#x5230;&#x4E00;&#x4E2A; bytes&#xFF0C;&#x7136;&#x540E;&#x7528; <code>pickle.loads()</code>&#x65B9;&#x6CD5;&#x53CD;&#x5E8F;&#x5217;&#x5316;&#x51FA;&#x5BF9;&#x8C61;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x7528; <code>pickle.load()</code>&#x65B9;&#x6CD5;&#x4ECE;&#x4E00;&#x4E2A; <code>file-like Object</code> &#x4E2D;&#x76F4;&#x63A5;&#x53CD;&#x5E8F;&#x5217;&#x5316;&#x51FA;&#x5BF9;&#x8C61;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>f = open(<span class="hljs-string">&apos;dump.txt&apos;</span>, <span class="hljs-string">&apos;rb&apos;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>d = pickle.load(f)
<span class="hljs-meta">&gt;&gt;&gt; </span>f.close()
<span class="hljs-meta">&gt;&gt;&gt; </span>d
{<span class="hljs-string">&apos;age&apos;</span>: <span class="hljs-number">20</span>, <span class="hljs-string">&apos;score&apos;</span>: <span class="hljs-number">88</span>, <span class="hljs-string">&apos;name&apos;</span>: <span class="hljs-string">&apos;Bob&apos;</span>}
</code></pre>
<blockquote>
<p>[&#x6CE8;]Pickle&#x662F;Python&#x7279;&#x6709;&#x7684;&#x3002;Pickle &#x7684;&#x95EE;&#x9898;&#x548C;&#x6240;&#x6709;&#x5176;&#x4ED6;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x7279;&#x6709;&#x7684;&#x5E8F;&#x5217;&#x5316;&#x95EE;&#x9898;&#x4E00;&#x6837;&#xFF0C;&#x5C31;&#x662F;&#x5B83;&#x53EA;&#x80FD;&#x7528;&#x4E8E; Python&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x80FD;&#x4E0D;&#x540C;&#x7248;&#x672C;&#x7684; Python &#x5F7C;&#x6B64;&#x90FD;&#x4E0D;&#x517C;&#x5BB9;&#xFF0C;&#x56E0;&#x6B64;&#xFF0C;&#x53EA;&#x80FD;&#x7528; Pickle &#x4FDD;&#x5B58;&#x90A3;&#x4E9B;&#x4E0D;&#x91CD;&#x8981;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x4E0D;&#x80FD;&#x6210;&#x529F;&#x5730;&#x53CD;&#x5E8F;&#x5217;&#x5316;&#x4E5F;&#x6CA1;&#x5173;&#x7CFB;&#x3002;</p>
</blockquote>
<h3 id="json">JSON</h3>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x8981;&#x5728;&#x4E0D;&#x540C;&#x7684;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x4E4B;&#x95F4;&#x4F20;&#x9012;&#x5BF9;&#x8C61;&#xFF0C;&#x5C31;&#x5FC5;&#x987B;&#x628A;&#x5BF9;&#x8C61;&#x5E8F;&#x5217;&#x5316;&#x4E3A;&#x6807;&#x51C6;&#x683C;&#x5F0F;&#xFF0C;&#x6BD4;&#x5982; XML&#xFF0C;&#x4F46;&#x66F4;&#x597D;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x5E8F;&#x5217;&#x5316;&#x4E3A; JSON&#xFF0C; &#x56E0;&#x4E3A;JSON&#x8868;&#x793A;&#x51FA;&#x6765;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x53EF;&#x4EE5;&#x88AB;&#x6240;&#x6709;&#x8BED;&#x8A00;&#x8BFB;&#x53D6;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x65B9;&#x4FBF;&#x5730;&#x5B58;&#x50A8;&#x5230;&#x78C1;&#x76D8;&#x6216;&#x8005;&#x901A;&#x8FC7;&#x7F51;&#x7EDC;&#x4F20;&#x8F93;&#x3002; JSON &#x4E0D;&#x4EC5;&#x662F;&#x6807;&#x51C6;&#x683C;&#x5F0F;&#xFF0C;&#x5E76;&#x4E14;&#x6BD4; XML &#x66F4;&#x5FEB;&#xFF0C;&#x800C;&#x4E14;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5728; Web &#x9875;&#x9762;&#x4E2D;&#x8BFB;&#x53D6;&#xFF0C;&#x975E;&#x5E38;&#x65B9;&#x4FBF;&#x3002;</p>
<p>JSON &#x8868;&#x793A;&#x7684;&#x5BF9;&#x8C61;&#x5C31;&#x662F;&#x6807;&#x51C6;&#x7684; JavaScript &#x8BED;&#x8A00;&#x7684;&#x5BF9;&#x8C61;&#xFF0C; JSON &#x548C; Python&#x5185;&#x7F6E;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x5BF9;&#x5E94;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>JSON&#x7C7B;&#x578B;       Python&#x7C7B;&#x578B;
   {}            dict
   []            list
&quot;string&quot;         str
1234.56        int &#x6216; float
true/false     True/False
  null           None
</code></pre><p>Python &#x5185;&#x7F6E;&#x7684; json &#x6A21;&#x5757;&#x63D0;&#x4F9B;&#x4E86;&#x975E;&#x5E38;&#x5B8C;&#x5584;&#x7684; Python &#x5BF9;&#x8C61;&#x5230; JSON &#x683C;&#x5F0F;&#x7684;&#x8F6C;&#x6362;&#x3002;&#x6211;&#x4EEC;&#x5148;&#x770B;&#x770B;&#x5982;&#x4F55;&#x628A; Python &#x5BF9;&#x8C61;&#x53D8;&#x6210;&#x4E00;&#x4E2A; JSON&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> json
<span class="hljs-meta">&gt;&gt;&gt; </span>d = dict(name=<span class="hljs-string">&apos;Bob&apos;</span>, age=<span class="hljs-number">20</span>, score=<span class="hljs-number">88</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>json.dumps(d)
<span class="hljs-string">&apos;{&quot;age&quot;: 20, &quot;score&quot;: 88, &quot;name&quot;: &quot;Bob&quot;}&apos;</span>
</code></pre>
<p><code>dumps()</code>&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; str&#xFF0C;&#x5185;&#x5BB9;&#x5C31;&#x662F;&#x6807;&#x51C6;&#x7684; JSON&#x3002;&#x7C7B;&#x4F3C;&#x7684;&#xFF0C; <code>dump()</code>&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x628A; JSON &#x5199;&#x5165;&#x4E00;&#x4E2A; <code>file-like Object</code>&#x3002;<br>&#x8981;&#x628A; JSON &#x53CD;&#x5E8F;&#x5217;&#x5316;&#x4E3A; Python &#x5BF9;&#x8C61;&#xFF0C;&#x7528; <code>loads()</code>&#x6216;&#x8005;&#x5BF9;&#x5E94;&#x7684; <code>load()</code>&#x65B9;&#x6CD5;&#xFF0C;&#x524D;&#x8005;&#x628A; JSON &#x7684;&#x5B57;&#x7B26;&#x4E32;&#x53CD;&#x5E8F;&#x5217;&#x5316;&#xFF0C;&#x540E;&#x8005;&#x4ECE; <code>file-like Object</code> &#x4E2D;&#x8BFB;&#x53D6;&#x5B57;&#x7B26;&#x4E32;&#x5E76;&#x53CD;&#x5E8F;&#x5217;&#x5316;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>json_str = <span class="hljs-string">&apos;{&quot;age&quot;: 20, &quot;score&quot;: 88, &quot;name&quot;: &quot;Bob&quot;}&apos;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>json.loads(json_str)
{<span class="hljs-string">&apos;age&apos;</span>: <span class="hljs-number">20</span>, <span class="hljs-string">&apos;score&apos;</span>: <span class="hljs-number">88</span>, <span class="hljs-string">&apos;name&apos;</span>: <span class="hljs-string">&apos;Bob&apos;</span>}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="异常处理.html" class="navigation navigation-prev " aria-label="Previous page: 异常处理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="模块.html" class="navigation navigation-next " aria-label="Next page: 模块">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"IO编程","level":"1.11","depth":1,"next":{"title":"模块","level":"1.12","depth":1,"path":"模块.md","ref":"模块.md","articles":[]},"previous":{"title":"异常处理","level":"1.10","depth":1,"path":"异常处理.md","ref":"异常处理.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"IO编程.md","mtime":"2017-08-23T14:57:31.617Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2017-08-27T04:11:18.522Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

